package org.lsp.opencvauto.image;

import org.opencv.core.*;
import org.opencv.core.Point;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class ImageDispose {

    private static final Logger log = LoggerFactory.getLogger(ImageDispose.class);





    /**
     * 图片模板匹配的位置
     * */
    public Map<String,Object> identification(String templateUrl,String  targetUrl){
        // 查找最佳匹配位置
        Map<String, Object> map = matchTemplate(templateUrl, targetUrl);
        Core.MinMaxLocResult mmr = Core.minMaxLoc((Mat) map.get("result"));
        map.put("minMaxLoc",mmr);
        log.info("匹配结果mmr.maxVal： "+mmr.maxVal);
        return  map;
    }

/**
 * 根据坐标和宽高截取图片
 * */
    public Mat imageScreenshot(Point maxLoc, Mat target, Mat templateGray){
        Rect rect =new Rect((int) maxLoc.x, (int) maxLoc.y,templateGray.cols(),templateGray.rows());
        return new Mat(target,rect);
    }


    public Mat imageScreenshot( Mat src, Core.MinMaxLocResult messagesXY,int width, int height){
       return new Mat(src, new Rect(
                (int) messagesXY.maxLoc.x,
                (int) messagesXY.maxLoc.y,
                width,
                height
        ));
    }

    /**
     * 图像匹配
     * */
    public static Map<String,Object> matchTemplate(String templateUrl,String  targetUrl){
        Map<String,Object> map=new HashMap<>();
        // 加载目标图像
        Mat target = Imgcodecs.imread(targetUrl);
        if (target.empty()) {
            System.err.println("目标图像加载失败！");
            return null;
        }

        // 加载模板图像
        ClassPathResource classPathResource = new ClassPathResource(templateUrl);
        Mat template = null;
        try {
            template = Imgcodecs.imread(classPathResource.getFile().getAbsolutePath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (template.empty()) {
            System.err.println("模板图像加载失败！");
            return null;
        }

        // 转换为灰度图
        Mat targetGray = new Mat();
        Mat templateGray = new Mat();
        Imgproc.cvtColor(target, targetGray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.cvtColor(template, templateGray, Imgproc.COLOR_BGR2GRAY);

        // 检查图像类型是否一致
        if (targetGray.type() != templateGray.type()) {
            System.err.println("图像类型不一致！");
            return null;
        }

        // 模板匹配
        Mat result = new Mat(
                targetGray.rows() - templateGray.rows() + 1,
                targetGray.cols() - templateGray.cols() + 1,
                CvType.CV_32FC1
        );
        Imgproc.matchTemplate(targetGray, templateGray, result, Imgproc.TM_CCOEFF_NORMED);
        map.put("result",result);
        map.put("src",target);
        map.put("templateGray",templateGray);
      return  map;
    }


}
